1 Introduction to Python
Hello Readers, here’s a quick, simple and kid-friendly introduction to Python:
Python is like a magic language that computers understand. It’s not as tricky as some other languages, making it perfect for beginners, including kids!What Python Can Do:
Talking to Computers: Imagine Python as a way to talk to computers in a language they understand. You can tell them what to do, and they’ll do it!
Making Games: Have you ever wanted to create your own video game? Python can help you do that. You can tell the computer how the game should work, and soon you’ll have your very own creation.
Drawing and Art: Python isn’t just for words; it can draw too! You can create beautiful pictures and designs with Python. It’s like having a digital paintbrush.
Solving Puzzles: Python is like a superhero when it comes to solving problems. You can use it to figure out puzzles and challenges. It’s your coding superhero!
Talking to Robots: Robots are cool, right? With Python, you can tell robots what to do. It’s like being a commander of your robot army!
Real-World Examples:
Weather Helper: Python can help you check the weather. You can create a program that tells you if it’s going to be sunny or rainy. No more surprises when you step outside!
Homework Assistant: Python can be your homework helper. You can make a program that solves math problems or checks your spelling. It’s like having a smart friend by your side.
Don’t take its literal meaning to solve your homework problem Instead take it as your mentor to ask for an explanation of your problem and ask it to give the working of the underlying solutions.
Virtual Pet: Ever wanted a pet dragon or a virtual friend? Python can help you create a virtual pet that you can feed, play with, and take care of. It’s like having a magical pet on your computer!
Treasure Hunt Game: Python can turn your computer into a treasure map. Create a game where you search for hidden treasures by giving commands to your Python friend. X marks the spot!
Remember, Python is like your secret code to make cool things happen on your computer. Have fun coding!
Finally, the purpose of the above setup was to do four things reliably while you work on the exercises:
- Write the exercise in your favorite text editor (mine VScode).
- Run the exercise.
- Fix the error when arises.
- Repeat
Anything else will only confuse you so stick to the plan.
1.1 Python Basics:
Open the search bar and open one of the following:
- Command Prompt (cmd) for short.
- PowerShell
- IDLE Python shell
- Open your favorite Editor and write the following code into it and then save it as hello.py (python programs can be run only if they are in .py extension)
Run your Python script in the directory where you have stored your .py python file just open cmd there and run the command: python filename.py
- 1
-
print
is called a function which will be explained in the letter chapters. so don’t stuck on this programming lingo for now. - 2
- As you can see between those yellow brackets, text can be enclosed in both single and double quotes.
- 3
- These quotes tell Python that we are writing strings and strings are like plain text to Python, it’s that simple.
Also, run your code in Python Visual Compiler which explains and runs your code step by step to show you what happens under the hood. Here’s the link: Python Visual Compiler
1.1.2 Numbers & Math in Python
Symbols | Names |
---|---|
+ |
Plus |
- |
Minus |
/ |
Slash |
* |
Asterisk |
% |
Percent |
< |
Less-than |
> |
Greater-than |
<= |
Less-than-equal |
>= |
Greater-than-equal |
1.1.3 Variable & Names
First, let’s understand Variable in simple terms:
Imagine you have a box, and you give it a name, like number_box
Now, you can put a number in it, let’s say 5. So, number_box
holds the value 5.
number_box
= 5
Here number_box
is the variable and the number 5 is the value in it. You can also change it like below
= 10
number_box print(number_box) # Output: 10
Now it contains 10 instead of 5. Variables are useful and it lets you store and keep track of your numbers text and several other things you will see in the advanced sections.
Quiz time:
Question 1: Write a Python code snippet to print the phrase “Hello, Python!”
Question 2: How can you print the result of multiplying two numbers, let’s say 4 and 7, in Python?
Question 3: Explain what the end parameter in the print function is used for.
Question 4: Write a Python code snippet to print your name on one line and your age on the next line.
Question 5: If you want to print the following text on separate lines:
Programmingis
! fun
How would you achieve this using the print function?
String formating
We can also format our string output in different ways, First I will write code and then from following you can see its explanation below:
= 'Asad Pro Beta' # My digital persona
my_name = 'Peshawar, Pakistan'
address = 35 # Just kidding
my_age = 74 # inches
my_height
1print(my_name+' ',address)
2print(my_name+3*'.')
3print('Your name is: {} my age: {}'.format(my_name,my_age))
4print('Your age is: {1} my name: {0}'.format(my_name, my_age))
5print(f'Your name is: {my_name} my age: {my_age}')
6print(r'Your name is: {my_name} my age: {my_age}')
- 1
- This + sign merges (concatenates) two strings together and that white space separates them
- 2
- First multiply the dot with 3 which becomes (…) & then concatenate (merge) it with that string (my_name)
- 3
- The format function in Python is like a template for creating sentences where you want to replace certain parts with specific values. Let me explain with an analogy: Imagine you have a fill-in-the-blank storybook. The storybook has sentences with blanks, and you have stickers with words on them. Each sticker has a different word that fits into one of the blanks.
- 4
- We can also use index numbers, Index are like the address of every variable where stored on computer memory.
- 5
- Putting f in the beginning acts as a format function with which it identifies variables.
- 6
- Putting r in the beginning means that this string is raw which means nothing in other words, print everything as it is.
1= [i for i in range(ord('A'),ord('E')+1)]
A,B,C,D,E = [i for i in range(ord('a'),ord('e')+1)]
a,b,c,d,e
2print(A,B,C,D,E,
a,b,c,d,e)
3= [65, 66, 67, 68, 69]
A, B, C, D, E
= "Mon Tue Wed Thu Fri Sat Sun"
days 4= "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
months
print(days)
print(months)
- 1
- This is another way to assign a list of values to multiple variables at once. (Don’t bother we will cover Lists in later chapters)
- 2
- Remove that comma after ‘E’ and see what happens
- 3
- It’s the same as the first one above, I have just created the list dynamically in the first place.
- 4
-
Here you will see that the
\n
character will act as an enter and printing will start from the new line. Copy and paste it into your editor and see its result on your own.
1.1.4 Escape Sequences
Imagine you’re writing a story on the computer, and you want to do some special things with your words. Escape sequences in Python are like magic codes you can use to make your words look different or do special tricks!
Look at the following table and try them out on your own:
Escape | What it does |
---|---|
\\ |
Backslash () |
\' |
Single-quote (’) |
\" |
Double-quote (“) |
\a |
ASCII bell (BEL) |
\b |
ASCII backspace (BS) |
\f |
ASCII formfeed (FF) |
\n |
ASCII linefeed (LF) |
\N{name} |
Character named name in the Unicode database |
\r |
ASCII carriage return (CR) |
\t |
ASCII horizontal tab (TAB) |
\uxxxx |
Character with 16-bit hex value xxxx (Unicode only) |
\Uxxxxxxxx |
Character with 32-bit hex value xxxxxxxx (Unicode only) |
\v |
ASCII vertical tab (VT) |
\ooo |
Character with octal value oo |
\xhh |
Character with hex value hh |
Prompting People or Asking for Input
print('How old are you?')
= input()
age print(f'Your age : {age}')
input()
is a method that shows you an empty placeholder for you to insert any value & when you insert then press enter so that the value can be stored in the age
variable.
Input()
function is like an empty page; someone gives you and you write anything that comes to your mind.
- Here, we have used the same string formatting as we used earlier.
1.1.5 Python modules
Let’s see the code first…
- 1
- os is a built-in module of Python that is used to do local operating system level operations like directory change, the path of the folder navigating through directories etc.
- 2
-
This code just copies the path where you are running your file. Don’t confuse yourself with that
os.getcwd()
for now you will soon understand this.
Asking Prompt and Passing Argument
from sys import argv
= argv
script_name, username print(f'Script name: {script_name}')
print(f'Username: {username}')
'''
Input: python delete.py asad
Output: Script name: delete.py
Username: Asad
'''
- This is the other way to import a module(feature) which says import from the module
sys
theargv
method (which is a small part of that module) - First open the terminal and navigate to your file where it is stored then run
python delete.py
asad heredelete.py
is my python file name write what’s yours andasad
is the argument like an extra input to catch it inside our program.
1.1.1 Comments in Python
Comments are used to tell what the specific part of your program does and it’s like an instruction box for your line of code. It can also be used to disable your code. so this it won’t run.